package cn.phecda.boot.data.jpa.entity;

import cn.phecda.boot.data.core.entity.DataStatus;
import cn.phecda.boot.data.core.entity.StatusEntity;
import cn.phecda.boot.data.core.exception.ReservedDataDeleteException;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;

/**
 * <p> 抽象状态实体 </p>
 *
 * @author : wanpinwei
 */
@MappedSuperclass
public abstract class AbstractStatusJpaEntity extends AbstractAuditJpaEntity implements StatusEntity {
    @Schema(description = "排序号")
    @Column(name = "sort_num")
    private Integer sortNum;

    @Schema(description = "数据状态")
    @Enumerated(EnumType.STRING)
    @Column(name = "data_status")
    private DataStatus dataStatus;

    @Schema(description = "是否保留数据, True 为不能删，False为可以删除")
    @Column(name = "reserved")
    private Boolean reserved;

    @Override
    public DataStatus getDataStatus() {
        return dataStatus;
    }

    @Override
    public void setDataStatus(DataStatus dataStatus) {
        this.dataStatus = dataStatus;
    }

    @Override
    public Boolean getReserved() {
        return reserved;
    }

    @Override
    public void setReserved(Boolean reserved) {
        this.reserved = reserved;
    }

    @Override
    public Integer getSortNum() {
        return sortNum;
    }

    @Override
    public void setSortNum(Integer sortNum) {
        this.sortNum = sortNum;
    }

    @PrePersist
    @PreUpdate
    public void preAction() {
        if (dataStatus == null) {
            dataStatus = DataStatus.ENABLE;
        }
        if (reserved == null) {
            reserved = false;
        }
        if (sortNum == null) {
            sortNum = 0;
        }
    }

    @PreRemove
    public void preReomve() {
        if (Boolean.TRUE.equals(reserved)) {
            throw new ReservedDataDeleteException();
        }
    }

}
